package com.ibm.commerce.order.facade.server.commands;

import com.ibm.commerce.beans.DataBeanManager;
import com.ibm.commerce.foundation.client.util.oagis.RelationalExpression;
import com.ibm.commerce.foundation.common.util.logging.LoggingHelper;
import com.ibm.commerce.foundation.server.util.oagis.SelectionCriteriaMapper;
import com.ibm.commerce.oagis9.datatypes.GetType;
import com.ibm.commerce.oagis9.datatypes.ShowType;
import com.ibm.commerce.order.beans.OrderListDataBean;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

public class FetchOrdersByStatusCmdImpl extends AbstractFetchOrdersSOICmdImpl
{
  private static final String COPYRIGHT = "(c) Copyright International Business Machines Corporation 1996,2008";
  private static final String CLASSNAME = "FetchOrdersByStatusCmdImpl";

  protected Collection fetchOrders(GetType get)
    throws Exception
  {
    String METHODNAME = "fetchOrders";
    if (LoggingHelper.isEntryExitTraceEnabled(LOGGER)) {
      LOGGER.entering("FetchOrdersByStatusCmdImpl", "fetchOrders");
    }
    SelectionCriteriaMapper selectionCriteria = new SelectionCriteriaMapper(get.getExpression());
    ArrayList vOrders = new ArrayList();
    Map mapQueryParameters = selectionCriteria.getXPathParameters();
    if (mapQueryParameters == null) {
      return vOrders;
    }

    RelationalExpression lRelExpr = (RelationalExpression)mapQueryParameters.get("Status.1");
    if (lRelExpr != null)
    {
      List lStatuses = lRelExpr.getValues();
      String strStatuses = buildOrderStatusList(lStatuses);

      if (LoggingHelper.isTraceEnabled(LOGGER)) {
        LOGGER.logp(LoggingHelper.DEFAULT_TRACE_LOG_LEVEL, "FetchOrdersByStatusCmdImpl", "fetchOrders", "Order statuses: " + 
          strStatuses);
      }
      BigInteger maxItems = get.getMaxItems();
      BigInteger startNumber = get.getRecordSetStartNumber();

      vOrders.clear();
      if (strStatuses.length() > 0)
      {
        OrderListDataBean bnOrderList = initOrderListDataBean();
        bnOrderList.setRetrievalOrderStatus(strStatuses);

        bnOrderList.setSortMethodId(1);

        DataBeanManager.activate(bnOrderList);

        vOrders.addAll(bnOrderList.getOrders());

        List arrOrders = getOrders(vOrders, startNumber, maxItems);

        int recordTotal = vOrders.size();

        int start = calcStart(recordTotal, startNumber);
        BigInteger pageSize = calcPageSize(recordTotal, maxItems, start);
        int end = calcEnd(recordTotal, start, pageSize);
        boolean isCompleteIndicator = end == recordTotal;

        getPagingParameters().setRecordSetCompleteIndicator(isCompleteIndicator);
        getPagingParameters().setRecordSetCount(BigInteger.valueOf(arrOrders.size()));
        getPagingParameters().setRecordSetStartNumber(BigInteger.valueOf(start));
        getPagingParameters().setRecordSetTotal(BigInteger.valueOf(recordTotal));

        vOrders.clear();
        vOrders.addAll(arrOrders);
      }
      else if (LoggingHelper.isTraceEnabled(LOGGER)) {
        LOGGER.logp(LoggingHelper.DEFAULT_TRACE_LOG_LEVEL, "FetchOrdersByStatusCmdImpl", "fetchOrders", 
          "The request does not specify the statuses of the orders to be returned.");
      }

    }

    if (LoggingHelper.isEntryExitTraceEnabled(LOGGER)) {
      LOGGER.exiting("FetchOrdersByStatusCmdImpl", "fetchOrders");
    }
    return vOrders;
  }
}